package com.drug.wms.service.impl;

import com.drug.common.utils.DateUtils;
import com.drug.common.utils.uuid.IdUtils;
import com.drug.wms.domain.BusSequenceConf;
import com.drug.wms.error.BusinessError;
import com.drug.wms.error.BusinessException;
import com.drug.wms.mapper.BusSequenceConfMapper;
import com.drug.wms.service.IBusSequenceConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

/**
 * 序列配置Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-04
 */
@Service
public class BusSequenceConfServiceImpl implements IBusSequenceConfService {
    @Autowired
    private BusSequenceConfMapper busSequenceConfMapper;

    /**
     * 查询序列配置
     *
     * @param id 序列配置主键
     * @return 序列配置
     */
    @Override
    public BusSequenceConf selectBusSequenceConfById(String id) {
        return busSequenceConfMapper.selectBusSequenceConfById(id);
    }

    /**
     * 查询序列配置列表
     *
     * @param busSequenceConf 序列配置
     * @return 序列配置
     */
    @Override
    public List<BusSequenceConf> selectBusSequenceConfList(BusSequenceConf busSequenceConf) {
        return busSequenceConfMapper.selectBusSequenceConfList(busSequenceConf);
    }

    /**
     * 新增序列配置
     *
     * @param busSequenceConf 序列配置
     * @return 结果
     */
    @Override
    public int insertBusSequenceConf(BusSequenceConf busSequenceConf) {
        return busSequenceConfMapper.insertBusSequenceConf(busSequenceConf);
    }

    /**
     * 修改序列配置
     *
     * @param busSequenceConf 序列配置
     * @return 结果
     */
    @Override
    public int updateBusSequenceConf(BusSequenceConf busSequenceConf) {
        return busSequenceConfMapper.updateBusSequenceConf(busSequenceConf);
    }

    /**
     * 批量删除序列配置
     *
     * @param ids 需要删除的序列配置主键
     * @return 结果
     */
    @Override
    public int deleteBusSequenceConfByIds(String[] ids) {
        return busSequenceConfMapper.deleteBusSequenceConfByIds(ids);
    }

    /**
     * 删除序列配置信息
     *
     * @param id 序列配置主键
     * @return 结果
     */
    @Override
    public int deleteBusSequenceConfById(String id) {
        return busSequenceConfMapper.deleteBusSequenceConfById(id);
    }


    /**
     * 不加事务
     * 以后考虑重试次数
     * 8位
     *
     * @param code
     * @return
     */
    @Override
    public String genSeqByCode(String code) {

        return genSeqByCode(code, 8);
    }

    @Override
    public String genSeqByCode(String code, int length) {

        BusSequenceConf row = busSequenceConfMapper.generateSequenceByCode(code);
        if (row == null) {
            //抛出异常
            throw new BusinessException(BusinessError.NO_ROW);
        }
        int position = Integer.parseInt(row.getStartNum());
        if (position > Integer.parseInt(row.getEndNum())) {
            //抛出异常
            throw new BusinessException(BusinessError.SEQ_FULL);
        }
        StringBuilder sb = new StringBuilder(length);
        IntStream.range(0, length).forEach(i -> sb.append("0"));

        String mid = String.valueOf(position + 1);
        String ss = sb + mid;//000000011110
        String newStartNum = ss.substring(ss.length() - 8);

        Map<String, Object> param = new HashMap<>(4);
        param.put("code", code);
        param.put("startNum", row.getStartNum());
        param.put("newNum", newStartNum);
        if (busSequenceConfMapper.updateStartNumByCodeV(param) > 0) {
            return row.getPrefix() + sb.append(row.getStartNum()).substring(sb.length() - length);
        }
        return genSeqByCode(code, length);
    }

    @Override
    public BusSequenceConf selectBusSequenceConfByCode(String code) {
        return busSequenceConfMapper.selectBusSequenceConfByCode(code);
    }
}